{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Selecting Series from DataFrame "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Single Series"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# conventional way to import pandas\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# read a dataset of UFO reports into DataFrame \n",
"ufo = pd.read_table('http://bit.ly/uforeports', sep=',')\n",
"\n",
"# read a csv is equivalent to read_table, except it assumes a comma separator \n",
"ufo = pd.read_csv('http://bit.ly/uforeports')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" City | \n",
" Colors Reported | \n",
" Shape Reported | \n",
" State | \n",
" Time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ithaca | \n",
" NaN | \n",
" TRIANGLE | \n",
" NY | \n",
" 6/1/1930 22:00 | \n",
"
\n",
" \n",
" 1 | \n",
" Willingboro | \n",
" NaN | \n",
" OTHER | \n",
" NJ | \n",
" 6/30/1930 20:00 | \n",
"
\n",
" \n",
" 2 | \n",
" Holyoke | \n",
" NaN | \n",
" OVAL | \n",
" CO | \n",
" 2/15/1931 14:00 | \n",
"
\n",
" \n",
" 3 | \n",
" Abilene | \n",
" NaN | \n",
" DISK | \n",
" KS | \n",
" 6/1/1931 13:00 | \n",
"
\n",
" \n",
" 4 | \n",
" New York Worlds Fair | \n",
" NaN | \n",
" LIGHT | \n",
" NY | \n",
" 4/18/1933 19:00 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" City Colors Reported Shape Reported State Time\n",
"0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00\n",
"1 Willingboro NaN OTHER NJ 6/30/1930 20:00\n",
"2 Holyoke NaN OVAL CO 2/15/1931 14:00\n",
"3 Abilene NaN DISK KS 6/1/1931 13:00\n",
"4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# examine first 5 rows \n",
"ufo.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 Ithaca\n",
"1 Willingboro\n",
"2 Holyoke\n",
"3 Abilene\n",
"4 New York Worlds Fair\n",
" ... \n",
"18236 Grant Park\n",
"18237 Spirit Lake\n",
"18238 Eagle River\n",
"18239 Eagle River\n",
"18240 Ybor\n",
"Name: City, Length: 18241, dtype: object"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# select 'City' Series using bracket notation\n",
"ufo['City']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(ufo['City'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 Ithaca\n",
"1 Willingboro\n",
"2 Holyoke\n",
"3 Abilene\n",
"4 New York Worlds Fair\n",
" ... \n",
"18236 Grant Park\n",
"18237 Spirit Lake\n",
"18238 Eagle River\n",
"18239 Eagle River\n",
"18240 Ybor\n",
"Name: City, Length: 18241, dtype: object"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# select 'City' Series using dot(.) notation\n",
"ufo.City"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Note__\n",
"- Bracket notation will always work, whereas dot notation has **limitations**\n",
"- Dot notation doesn't work if there are **spaces** in the Series name\n",
"- Dot notation doesn't work if the Series has the same name as a **DataFrame method or attribute** (like 'head' or 'shape')\n",
"- Dot notation can't be used to define the name of a **new Series** (see below)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" City | \n",
" Colors Reported | \n",
" Shape Reported | \n",
" State | \n",
" Time | \n",
" Location | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ithaca | \n",
" NaN | \n",
" TRIANGLE | \n",
" NY | \n",
" 6/1/1930 22:00 | \n",
" Ithaca, NY | \n",
"
\n",
" \n",
" 1 | \n",
" Willingboro | \n",
" NaN | \n",
" OTHER | \n",
" NJ | \n",
" 6/30/1930 20:00 | \n",
" Willingboro, NJ | \n",
"
\n",
" \n",
" 2 | \n",
" Holyoke | \n",
" NaN | \n",
" OVAL | \n",
" CO | \n",
" 2/15/1931 14:00 | \n",
" Holyoke, CO | \n",
"
\n",
" \n",
" 3 | \n",
" Abilene | \n",
" NaN | \n",
" DISK | \n",
" KS | \n",
" 6/1/1931 13:00 | \n",
" Abilene, KS | \n",
"
\n",
" \n",
" 4 | \n",
" New York Worlds Fair | \n",
" NaN | \n",
" LIGHT | \n",
" NY | \n",
" 4/18/1933 19:00 | \n",
" New York Worlds Fair, NY | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" City Colors Reported Shape Reported State Time \\\n",
"0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00 \n",
"1 Willingboro NaN OTHER NJ 6/30/1930 20:00 \n",
"2 Holyoke NaN OVAL CO 2/15/1931 14:00 \n",
"3 Abilene NaN DISK KS 6/1/1931 13:00 \n",
"4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00 \n",
"\n",
" Location \n",
"0 Ithaca, NY \n",
"1 Willingboro, NJ \n",
"2 Holyoke, CO \n",
"3 Abilene, KS \n",
"4 New York Worlds Fair, NY "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create a new 'Location' Series (must use bracket notation to define the Series name)\n",
"ufo['Location'] = ufo.City + ', ' + ufo.State\n",
"ufo.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiple Series"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" City | \n",
" State | \n",
" Time | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ithaca | \n",
" NY | \n",
" 6/1/1930 22:00 | \n",
"
\n",
" \n",
" 1 | \n",
" Willingboro | \n",
" NJ | \n",
" 6/30/1930 20:00 | \n",
"
\n",
" \n",
" 2 | \n",
" Holyoke | \n",
" CO | \n",
" 2/15/1931 14:00 | \n",
"
\n",
" \n",
" 3 | \n",
" Abilene | \n",
" KS | \n",
" 6/1/1931 13:00 | \n",
"
\n",
" \n",
" 4 | \n",
" New York Worlds Fair | \n",
" NY | \n",
" 4/18/1933 19:00 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 18236 | \n",
" Grant Park | \n",
" IL | \n",
" 12/31/2000 23:00 | \n",
"
\n",
" \n",
" 18237 | \n",
" Spirit Lake | \n",
" IA | \n",
" 12/31/2000 23:00 | \n",
"
\n",
" \n",
" 18238 | \n",
" Eagle River | \n",
" WI | \n",
" 12/31/2000 23:45 | \n",
"
\n",
" \n",
" 18239 | \n",
" Eagle River | \n",
" WI | \n",
" 12/31/2000 23:45 | \n",
"
\n",
" \n",
" 18240 | \n",
" Ybor | \n",
" FL | \n",
" 12/31/2000 23:59 | \n",
"
\n",
" \n",
"
\n",
"
18241 rows × 3 columns
\n",
"
"
],
"text/plain": [
" City State Time\n",
"0 Ithaca NY 6/1/1930 22:00\n",
"1 Willingboro NJ 6/30/1930 20:00\n",
"2 Holyoke CO 2/15/1931 14:00\n",
"3 Abilene KS 6/1/1931 13:00\n",
"4 New York Worlds Fair NY 4/18/1933 19:00\n",
"... ... ... ...\n",
"18236 Grant Park IL 12/31/2000 23:00\n",
"18237 Spirit Lake IA 12/31/2000 23:00\n",
"18238 Eagle River WI 12/31/2000 23:45\n",
"18239 Eagle River WI 12/31/2000 23:45\n",
"18240 Ybor FL 12/31/2000 23:59\n",
"\n",
"[18241 rows x 3 columns]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# select multiple series from dataframe \n",
"ufo[['City', 'State', 'Time']]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}